package com.example.speedwaymanager;

public class CRider {

	private int _rank;
	private String _name;
	private String _team;
	private Float _avg;
	private Float _avgHome;
	private Float _avgAway;
	private float _matchAvg;
	private ERiderType _type;

	private CHeatResult _heatResult = new CHeatResult();
	private CMatchStats _matchStats = new CMatchStats();

	public CRider(String rank, String name, String team, String avg, String avgHome,
			String avgAway, String type) {
		_rank = Integer.parseInt(rank);
		_name = shortName(name);
		_team = team;
		_avg = Float.parseFloat(avg);
		_avgHome = Float.parseFloat(avgHome);
		_avgAway = Float.parseFloat(avgAway);
		_type = translateRiderType(type);
	}

	/**
	 * translates string to enum type
	 * 
	 * @param type
	 * @return
	 */
	private ERiderType translateRiderType(String type) {
		ERiderType eType = null;
		if (type.equals("SENIOR")) {
			eType = ERiderType.eSenior;
		} else if (type.equals("JUNIOR")) {
			eType = ERiderType.eJunior;
		} else if (type.equals("FOREGIN")) {
			eType = ERiderType.eForegin;
		}
		return eType;
	}

	/**
	 * shortens the full name to just first letter of name and full surname
	 * @param name
	 * @return first letter.surname
	 */
	private String shortName(String name) {
		String str = name;
		int spacePos = str.lastIndexOf(" ");
		String surname = str.substring(spacePos+1, str.length());
		str = str.charAt(0) + "." + surname;
		return str;
	}
	public void clearPoints() {
		_matchStats.get_pointsList().clear();
		_matchStats.clearPointsAndBonuses();
		_matchStats.set_tacticalAllowed(true);
	}


	/**
	 * @return the name and the number for logging purposes
	 */
	public String get_numberAndName() {
		return _matchStats.get_number().toString() + "." + _name; 
	}
	
	public String get_numberAndNameAndStats() {
		return _matchStats.get_number().toString() + "." + _name + ", " +
			   _matchStats.get_tacticalAllowed().toString() + ", " +
			   _matchStats.get_points().toString() + "pts, " +
			   _matchStats.get_bonuses().toString() + "B, " +
			   get_matchAvg().toString();
	}
	
	/**
	 * @return get the number 
	 */
	public Integer get_number() {
		return _matchStats.get_number(); 
	}
	
	/**
	 * @return the _team
	 */
	public String get_team() {
		return _team;
	}

	/**
	 * @return the _name
	 */
	public String get_name() {
		return _name;
	}

	/**
	 * @return the _avg
	 */
	public Float get_avg() {
		return _avg;
	}

	/**
	 * @return the _avgHome
	 */
	public Float get_avgHome() {
		return _avgHome;
	}

	/**
	 * @return the _avgAway
	 */
	public Float get_avgAway() {
		return _avgAway;
	}

	/**
	 * @return the _matchAvg
	 */
	public Float get_matchAvg() {
		return _matchAvg;
	}

	/**
	 * @param _matchAvg
	 *            the _matchAvg to set
	 */
	public void set_matchAvg(float _matchAvg) {
		this._matchAvg = _matchAvg;
	}

	/**
	 * @return the _type
	 */
	public ERiderType get_type() {
		return _type;
	}

	/**
	 * @param _type
	 *            the _type to set
	 */
	public void set_type(ERiderType _type) {
		this._type = _type;
	}

	/**
	 * @return the _matchStats
	 */
	public CMatchStats get_matchStats() {
		return _matchStats;
	}
	
	/**
	 * 
	 */
	public String toString()
	{
		return _name;				
	}

	/**
	 * @return the _heatResult
	 */
	public CHeatResult get_heatResult() {
		return _heatResult;
	}
}
